package com.leyou.user.web;

import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/info")
public class UserController {
    @Autowired
    private UserService userService;

    //判断信息是否存在

    @GetMapping("/exists/{data}/{type}")
    public ResponseEntity<Boolean> checkExist(@PathVariable("data")String data,
            @PathVariable("type")Integer type){
        return ResponseEntity.ok(this.userService.checkExist(data,type));
    }

    //发送短信验证码
    @PostMapping("/code")
    public ResponseEntity<Void> sendVerifyCode(@RequestParam("phone")String phone){
        this.userService.sendVerifyCode(phone);
        return ResponseEntity.ok().build();
    }

    //注册成为新用户
    @PostMapping
    public ResponseEntity<Void>  register(@Valid User user , @RequestParam("code") String code){
        this.userService.register(user,code);
        return ResponseEntity.ok().build();
    }


   //根据用户名和密码查询
    @GetMapping
    public ResponseEntity<UserDTO> queryUserNameAndPass(
            @RequestParam("username")String username,
            @RequestParam("password")String password
    ){
        return ResponseEntity.ok(this.userService.queryUserNameAndPass(username,password));
    }
}
